package com.syqc.monitor.ui.newaddcar

import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.rx.doAfterChanged
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.AppContext
import com.syqc.entity.VideoPass
import com.syqc.monitor.R
import com.syqc.monitor.databinding.IncludeNegativePositiveBinding
import com.syqc.monitor.databinding.PopupPassBinding
import com.syqc.monitor.databinding.PopupVideoPassBinding

/**
 *@author  hfjs
 *@time   2024/8/27 9:40
 *@desc
 */
class PopupVideoPass(private val context: Context, private val callback: (String?) -> Unit) :
    PopupWindow() {
    private val binding = PopupVideoPassBinding.inflate(LayoutInflater.from(context))
    private var mAdapter: VideoPassAdapter? = null
    private var mPNDing: IncludeNegativePositiveBinding? = null

    //选中的通道下标
    private var index = -1
    private val passPopup by lazy {
        PopupPass(context) {
            mAdapter?.run {
                VideoPassUtil.setPass(index, data[index].pass, it)
                data[index].pass = it
                notifyItemChanged(index)
//                notifyDataSetChanged()
            }

        }
    }

    init {
        contentView = binding.root
        mPNDing = IncludeNegativePositiveBinding.bind(binding.root)
        initView()
        width = LinearLayout.LayoutParams.MATCH_PARENT
        height = LinearLayout.LayoutParams.MATCH_PARENT
        isFocusable = true
        setBackgroundDrawable(ColorDrawable(-0x56000000)) //设置背景
        animationStyle = R.style.PopupAnimation
        this.isClippingEnabled = false
    }

    private fun initView() {
        binding.rvPass.layoutManager = LinearLayoutManager(context)
        binding.rvPass.adapter = mAdapter

        mPNDing!!.tvNegative.filterFast {
            callback(VideoPassUtil.getVideoPass(mAdapter?.data))
            dismiss()
            mAdapter = null
        }

        mPNDing!!.tvPositive.filterFast {
            dismiss()
            mAdapter = null
        }


    }

    fun showView(view: View, num: Int, videos: String?) {
        val passList = if (videos == null) {
            VideoPassUtil.setPassNum(num)
        } else {
            VideoPassUtil.handlePass(videos)
        }
        mAdapter = VideoPassAdapter()
        binding.rvPass.adapter = mAdapter
        index = -1
        mAdapter?.setList(passList)
        mAdapter?.setOnItemChildClickListener { adapter, view, position ->
            index = position
            passPopup.showView(view, VideoPassUtil.getNoChooseKey())
        }
        showAtLocation(view, Gravity.NO_GRAVITY, 0, 0)
    }
}

class VideoPassAdapter : BaseQuickAdapter<VideoPass, BaseViewHolder>(R.layout.item_video_pass) {
    private val pass by lazy { AppContext.getDescById(R.string.desc_video_pass) }

    init {
        addChildClickViewIds(R.id.tvPass)
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun convert(holder: BaseViewHolder, item: VideoPass) {
        val tvPass = holder.getView<TextView>(R.id.tvPass)
        val editPass = holder.getView<EditText>(R.id.editPass)
        tvPass.text = item.pass.toString()
        if (item.passName.isNullOrEmpty()) {
            editPass.setText("")
            editPass.hint = "$pass ${item.pass}"
        } else {
            editPass.setText(item.passName)
            editPass.hint = item.passName
            editPass.setSelection(editPass.text.length)
        }

        editPass.doAfterChanged {
            item.passName = it
            VideoPassUtil.setPassName(holder.layoutPosition, item.passName ?: "")
        }
    }
}

//通道选择弹窗
class PopupPass(private val context: Context, private val callback: (Int) -> Unit) : PopupWindow() {
    private val mAdapter by lazy { PassAdapter() }
    private val binding = PopupPassBinding.inflate(LayoutInflater.from(context))

    init {
        contentView = binding.root
        isClippingEnabled = false
        initView()
        width = LinearLayout.LayoutParams.WRAP_CONTENT
        height = LinearLayout.LayoutParams.WRAP_CONTENT
        isFocusable = true //popupwindow设置焦点
        animationStyle = R.style.PopupAnimation
        this.isClippingEnabled = false
    }

    private fun initView() {
        binding.rvPass.layoutManager = LinearLayoutManager(context)
        binding.rvPass.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            callback(mAdapter.data[position])
            dismiss()
        }
    }

    fun showView(view: View, passs: MutableList<Int>) {
        mAdapter.setList(passs)
        showAsDropDown(view)
    }
}

class PassAdapter : BaseQuickAdapter<Int, BaseViewHolder>(R.layout.item_pass) {
    override fun convert(holder: BaseViewHolder, item: Int) {
        holder.setText(R.id.tvPass, item.toString())
    }
}